<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 5.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" integrity="sha256-xejo6yLi6vGtAjcMIsY8BHdKsLg7QynVlFMzdQgUuy8=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css" integrity="sha256-Vzbj7sDDS/woiFS3uNKo8eIuni59rjyNGtXfstRzStA=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"gz1234.gitee.io","root":"/","images":"/images","scheme":"Gemini","darkmode":true,"version":"8.12.3","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"flat"},"bookmark":{"enable":true,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="Vue2 编码基础vue 项目规范以 Vue 官方规范 （https:&#x2F;&#x2F;cn.vuejs.org&#x2F;v2&#x2F;style-guide&#x2F;） 中的 A 规范为基础，在其上面进行项目开发，故所有代码均遵守该规范。  请仔仔细细阅读 Vue 官方规范，切记，此为第一步。  组件名为多个单词，而不是一个单词组件名应该始终是多个单词组成（大于等于 2），且命名规范为KebabCase格式。这样做可以避免跟现有的以">
<meta property="og:type" content="website">
<meta property="og:title" content="郭泽">
<meta property="og:url" content="https://gz1234.gitee.io/spec-master/vue%E4%BB%A3%E7%A0%81%E8%A7%84%E8%8C%83.html">
<meta property="og:site_name" content="郭泽">
<meta property="og:description" content="Vue2 编码基础vue 项目规范以 Vue 官方规范 （https:&#x2F;&#x2F;cn.vuejs.org&#x2F;v2&#x2F;style-guide&#x2F;） 中的 A 规范为基础，在其上面进行项目开发，故所有代码均遵守该规范。  请仔仔细细阅读 Vue 官方规范，切记，此为第一步。  组件名为多个单词，而不是一个单词组件名应该始终是多个单词组成（大于等于 2），且命名规范为KebabCase格式。这样做可以避免跟现有的以">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-05-31T02:54:55.174Z">
<meta property="article:modified_time" content="2023-05-31T00:57:28.727Z">
<meta property="article:author" content="郭泽">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://gz1234.gitee.io/spec-master/vue%E4%BB%A3%E7%A0%81%E8%A7%84%E8%8C%83">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":false,"lang":"zh-CN","comments":true,"permalink":"https://gz1234.gitee.io/spec-master/vue%E4%BB%A3%E7%A0%81%E8%A7%84%E8%8C%83.html","path":"spec-master/vue代码规范.html","title":""}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title> | 郭泽
</title>
  





  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">郭泽</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Vue2-%E7%BC%96%E7%A0%81%E5%9F%BA%E7%A1%80"><span class="nav-number">1.</span> <span class="nav-text">Vue2 编码基础</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%84%E4%BB%B6%E5%90%8D%E4%B8%BA%E5%A4%9A%E4%B8%AA%E5%8D%95%E8%AF%8D%EF%BC%8C%E8%80%8C%E4%B8%8D%E6%98%AF%E4%B8%80%E4%B8%AA%E5%8D%95%E8%AF%8D"><span class="nav-number">1.1.</span> <span class="nav-text">组件名为多个单词，而不是一个单词</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%84%E4%BB%B6%E7%9A%84data%E5%BF%85%E9%A1%BB%E6%98%AF%E4%B8%80%E4%B8%AA%E5%87%BD%E6%95%B0"><span class="nav-number">1.2.</span> <span class="nav-text">组件的data必须是一个函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#prop%E7%9A%84%E5%AE%9A%E4%B9%89%E5%BA%94%E8%AF%A5%E5%B0%BD%E9%87%8F%E8%AF%A6%E7%BB%86%EF%BC%8C%E8%87%B3%E5%B0%91%E9%9C%80%E8%A6%81%E6%8C%87%E5%AE%9A%E7%B1%BB%E5%9E%8B"><span class="nav-number">1.3.</span> <span class="nav-text">prop的定义应该尽量详细，至少需要指定类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%BAv-for%E8%AE%BE%E7%BD%AE%E9%94%AE%E5%80%BC%EF%BC%8C%E6%80%BB%E6%98%AF%E7%94%A8key%E9%85%8D%E5%90%88v-for"><span class="nav-number">1.4.</span> <span class="nav-text">为v-for设置键值，总是用key配合v-for</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B0%B8%E8%BF%9C%E4%B8%8D%E8%A6%81%E6%8A%8Av-if%E4%B8%8Ev-for%E7%94%A8%E5%9C%A8%E5%90%8C%E4%B8%80%E4%B8%AA%E5%85%83%E7%B4%A0%E4%B8%8A"><span class="nav-number">1.5.</span> <span class="nav-text">永远不要把v-if与v-for用在同一个元素上</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%BA%E7%BB%84%E4%BB%B6%E6%A0%B7%E5%BC%8F%E8%AE%BE%E7%BD%AE%E4%BD%9C%E7%94%A8%E5%9F%9F-scoped"><span class="nav-number">1.6.</span> <span class="nav-text">为组件样式设置作用域 scoped</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8D%95%E6%96%87%E4%BB%B6%E7%9A%84%E7%BB%84%E4%BB%B6%E5%90%8D%E4%B8%80%E5%AE%9A%E6%98%AF%E5%A4%A7%E5%86%99%E5%BC%80%E5%A4%B4%E6%88%96%E8%80%85%E4%BB%A5%E2%80%9C-%E2%80%9D%E8%BF%9E%E6%8E%A5"><span class="nav-number">1.7.</span> <span class="nav-text">单文件的组件名一定是大写开头或者以“-”连接</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%89%B9%E5%AE%9A%E6%A0%B7%E5%BC%8F%E6%88%96%E5%9F%BA%E7%A1%80%E7%BB%84%E4%BB%B6%EF%BC%8C%E5%BA%94%E8%AF%A5%E4%BB%A5%E5%90%8C%E4%B8%80%E4%B8%AA%E5%85%AC%E5%85%B1%E5%89%8D%E7%BC%80%E5%BC%80%E5%A4%B4"><span class="nav-number">1.8.</span> <span class="nav-text">特定样式或基础组件，应该以同一个公共前缀开头</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A6%82%E6%9E%9C%E4%B8%80%E4%B8%AA%E7%BB%84%E4%BB%B6%E6%98%AF%E4%B8%80%E4%B8%AA%E5%AE%9A%E5%88%B6%E7%BB%84%E4%BB%B6%EF%BC%8C%E5%8D%B3%E5%AE%83%E4%B8%8D%E6%98%AF%E4%B8%80%E4%B8%AA%E5%85%AC%E5%85%B1%E7%BB%84%E4%BB%B6%EF%BC%8C%E8%BF%99%E7%A7%8D%E7%BB%84%E4%BB%B6%E5%90%8D%E5%BA%94%E4%BB%A5The%E5%BC%80%E5%A4%B4%EF%BC%8C%E8%A1%A8%E7%A4%BA%E5%94%AF%E4%B8%80%E6%80%A7%EF%BC%8C%E5%AE%83%E4%B8%8D%E5%BA%94%E8%AF%A5%E6%8E%A5%E6%94%B6%E4%BB%BB%E4%BD%95prop%EF%BC%8C%E5%A6%82%E6%9E%9C%E6%8E%A5%E6%94%B6prop%E9%82%A3%E4%B9%88%E8%AF%B4%E6%98%8E%E5%AE%83%E6%98%AF%E4%B8%80%E4%B8%AA%E5%85%AC%E5%85%B1%E7%BB%84%E4%BB%B6"><span class="nav-number">1.9.</span> <span class="nav-text">如果一个组件是一个定制组件，即它不是一个公共组件，这种组件名应以The开头，表示唯一性，它不应该接收任何prop，如果接收prop那么说明它是一个公共组件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%92%8C%E7%88%B6%E7%BB%84%E4%BB%B6%E7%B4%A7%E5%AF%86%E8%80%A6%E5%90%88%E5%AD%90%E7%BB%84%E4%BB%B6%EF%BC%8C%E5%BA%94%E4%BB%A5%E7%88%B6%E7%BB%84%E4%BB%B6%E5%90%8D%E4%BD%9C%E4%B8%BA%E5%89%8D%E7%BC%80%E5%90%8D"><span class="nav-number">1.10.</span> <span class="nav-text">和父组件紧密耦合子组件，应以父组件名作为前缀名</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9C%A8%E5%8D%95%E6%96%87%E4%BB%B6%E7%BB%84%E4%BB%B6%E3%80%81%E5%AD%97%E7%AC%A6%E6%A8%A1%E6%9D%BF%E5%92%8CJSX%E4%B8%AD%E6%B2%A1%E6%9C%89%E5%86%85%E5%AE%B9%E7%9A%84%E7%BB%84%E4%BB%B6%E5%BA%94%E8%AF%A5%E6%98%AF%E8%87%AA%E9%97%AD%E5%92%8C%E7%9A%84%EF%BC%8C%E4%BD%86%E6%98%AF%E5%9C%A8DOM%E6%A8%A1%E6%9D%BF%E9%87%8C%E6%B0%B8%E8%BF%9C%E4%B8%8D%E8%A6%81%E8%BF%99%E4%B9%88%E5%81%9A"><span class="nav-number">1.11.</span> <span class="nav-text">在单文件组件、字符模板和JSX中没有内容的组件应该是自闭和的，但是在DOM模板里永远不要这么做</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#JS-JSX%E4%B8%AD%E7%9A%84%E7%BB%84%E4%BB%B6%E5%90%8D%E5%BA%94%E4%B8%BA%E5%A4%A7%E9%A9%BC%E5%B3%B0%E5%91%BD%E5%90%8D"><span class="nav-number">1.12.</span> <span class="nav-text">JS&#x2F;JSX中的组件名应为大驼峰命名</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%9A%E4%B8%AAattribute%E7%9A%84%E5%85%83%E7%B4%A0%E5%BA%94%E8%AF%A5%E5%88%86%E5%A4%9A%E8%A1%8C%E6%92%B0%E5%86%99%EF%BC%8C%E6%AF%8F%E4%B8%AAattribute%E4%B8%80%E8%A1%8C"><span class="nav-number">1.13.</span> <span class="nav-text">多个attribute的元素应该分多行撰写，每个attribute一行</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%84%E4%BB%B6%E6%A8%A1%E6%9D%BF%E5%BA%94%E8%AF%A5%E5%8F%AA%E5%8C%85%E5%90%AB%E7%AE%80%E5%8D%95%E7%9A%84%E8%A1%A8%E8%BE%BE%E5%BC%8F%EF%BC%8C%E5%A4%8D%E6%9D%82%E7%9A%84%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%BA%94%E8%AF%A5%E9%87%8D%E6%9E%84%E4%B8%BA%E8%AE%A1%E7%AE%97%E5%B1%9E%E6%80%A7%E6%88%96%E6%96%B9%E6%B3%95"><span class="nav-number">1.14.</span> <span class="nav-text">组件模板应该只包含简单的表达式，复杂的表达式应该重构为计算属性或方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%9D%9E%E7%A9%BA%E7%9A%84attitude%E5%80%BC%E5%BA%94%E8%AF%A5%E5%A7%8B%E7%BB%88%E5%B8%A6%E5%BC%95%E5%8F%B7"><span class="nav-number">1.15.</span> <span class="nav-text">非空的attitude值应该始终带引号</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8C%87%E4%BB%A4%E5%8F%AF%E4%BB%A5%E7%BC%A9%E5%86%99%E7%9A%84%E5%BB%BA%E8%AE%AE%E7%BC%A9%E5%86%99"><span class="nav-number">1.16.</span> <span class="nav-text">指令可以缩写的建议缩写</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">郭泽</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">54</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">21</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">51</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner page posts-expand">


    
    
    
    <div class="post-block" lang="zh-CN"><header class="post-header">

<h1 class="post-title" itemprop="name headline">
</h1>

<div class="post-meta-container">
</div>

</header>

      
      
      <div class="post-body">
          <h2 id="Vue2-编码基础"><a href="#Vue2-编码基础" class="headerlink" title="Vue2 编码基础"></a>Vue2 编码基础</h2><p>vue 项目规范以 Vue 官方规范 （<a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/style-guide/">https://cn.vuejs.org/v2/style-guide/</a>） 中的 A 规范为基础，在其上面进行项目开发，故所有代码均遵守该规范。</p>
<blockquote>
<p>请仔仔细细阅读 Vue 官方规范，切记，此为第一步。</p>
</blockquote>
<h3 id="组件名为多个单词，而不是一个单词"><a href="#组件名为多个单词，而不是一个单词" class="headerlink" title="组件名为多个单词，而不是一个单词"></a>组件名为多个单词，而不是一个单词</h3><p>组件名应该始终是多个单词组成（大于等于 2），且命名规范为KebabCase格式。<br>这样做可以避免跟现有的以及未来的 HTML 元素相冲突，因为所有的 HTML 元素名称都是单个单词的。<br>正例</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Vue.component(<span class="string">&#x27;todo-item&#x27;</span>, &#123;</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  name: <span class="string">&#x27;TodoItem&#x27;</span>,</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="组件的data必须是一个函数"><a href="#组件的data必须是一个函数" class="headerlink" title="组件的data必须是一个函数"></a>组件的data必须是一个函数</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="function"><span class="title">data</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      name: <span class="string">&#x27;dzy&#x27;</span>    </span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="prop的定义应该尽量详细，至少需要指定类型"><a href="#prop的定义应该尽量详细，至少需要指定类型" class="headerlink" title="prop的定义应该尽量详细，至少需要指定类型"></a>prop的定义应该尽量详细，至少需要指定类型</h3><ul>
<li>必须使用 camelCase 驼峰命名</li>
<li>必须指定类型</li>
<li>必须加上注释，表明其含义</li>
<li>必须加上 required 或者 default，两者二选其一</li>
<li>如果有业务需要，必须加上 validator 验证</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"> props: &#123;</span><br><span class="line">  <span class="comment">// 组件状态，用于控制组件的颜色</span></span><br><span class="line">   status: &#123;</span><br><span class="line">     type: <span class="built_in">String</span>,</span><br><span class="line">     required: <span class="literal">true</span>,</span><br><span class="line">     validator: <span class="function"><span class="keyword">function</span> (<span class="params">value</span>) </span>&#123;</span><br><span class="line">       <span class="keyword">return</span> [</span><br><span class="line">         <span class="string">&#x27;succ&#x27;</span>,</span><br><span class="line">         <span class="string">&#x27;info&#x27;</span>,</span><br><span class="line">         <span class="string">&#x27;error&#x27;</span></span><br><span class="line">       ].indexOf(value) !== -<span class="number">1</span></span><br><span class="line">     &#125;</span><br><span class="line">   &#125;,</span><br><span class="line">    <span class="comment">// 用户级别，用于显示皇冠个数</span></span><br><span class="line">   userLevel：&#123;</span><br><span class="line">      type: <span class="built_in">String</span>,</span><br><span class="line">      required: <span class="literal">true</span></span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="为v-for设置键值，总是用key配合v-for"><a href="#为v-for设置键值，总是用key配合v-for" class="headerlink" title="为v-for设置键值，总是用key配合v-for"></a>为v-for设置键值，总是用key配合v-for</h3><p>在有状态的情况下 key 能使用index</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;ul&gt;</span><br><span class="line">  &lt;li</span><br><span class="line">    v-<span class="keyword">for</span>=<span class="string">&quot;todo in todos&quot;</span></span><br><span class="line">    :key=<span class="string">&quot;todo.id&quot;</span></span><br><span class="line">  &gt;</span><br><span class="line">    &#123;&#123; todo.text &#125;&#125;</span><br><span class="line">  &lt;/li&gt;</span><br><span class="line">&lt;/ul&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="永远不要把v-if与v-for用在同一个元素上"><a href="#永远不要把v-if与v-for用在同一个元素上" class="headerlink" title="永远不要把v-if与v-for用在同一个元素上"></a>永远不要把v-if与v-for用在同一个元素上</h3><h3 id="为组件样式设置作用域-scoped"><a href="#为组件样式设置作用域-scoped" class="headerlink" title="为组件样式设置作用域 scoped"></a>为组件样式设置作用域 scoped</h3><h3 id="单文件的组件名一定是大写开头或者以“-”连接"><a href="#单文件的组件名一定是大写开头或者以“-”连接" class="headerlink" title="单文件的组件名一定是大写开头或者以“-”连接"></a>单文件的组件名一定是大写开头或者以“-”连接</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">components/</span><br><span class="line">|- MyComponent.vue</span><br><span class="line">components/</span><br><span class="line">|- my-component.vue</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="特定样式或基础组件，应该以同一个公共前缀开头"><a href="#特定样式或基础组件，应该以同一个公共前缀开头" class="headerlink" title="特定样式或基础组件，应该以同一个公共前缀开头"></a>特定样式或基础组件，应该以同一个公共前缀开头</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">components/</span><br><span class="line">|- BaseButton.vue</span><br><span class="line">|- BaseTable.vue</span><br><span class="line">|- BaseIcon.vue</span><br><span class="line">components/</span><br><span class="line">|- AppButton.vue</span><br><span class="line">|- AppTable.vue</span><br><span class="line">|- AppIcon.vue</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="如果一个组件是一个定制组件，即它不是一个公共组件，这种组件名应以The开头，表示唯一性，它不应该接收任何prop，如果接收prop那么说明它是一个公共组件"><a href="#如果一个组件是一个定制组件，即它不是一个公共组件，这种组件名应以The开头，表示唯一性，它不应该接收任何prop，如果接收prop那么说明它是一个公共组件" class="headerlink" title="如果一个组件是一个定制组件，即它不是一个公共组件，这种组件名应以The开头，表示唯一性，它不应该接收任何prop，如果接收prop那么说明它是一个公共组件"></a>如果一个组件是一个定制组件，即它不是一个公共组件，这种组件名应以The开头，表示唯一性，它不应该接收任何prop，如果接收prop那么说明它是一个公共组件</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">components/</span><br><span class="line">|- TheHeading.vue</span><br><span class="line">|- TheSidebar.vue</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="和父组件紧密耦合子组件，应以父组件名作为前缀名"><a href="#和父组件紧密耦合子组件，应以父组件名作为前缀名" class="headerlink" title="和父组件紧密耦合子组件，应以父组件名作为前缀名"></a>和父组件紧密耦合子组件，应以父组件名作为前缀名</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">components/</span><br><span class="line">|- TodoList.vue</span><br><span class="line">|- TodoListItem.vue</span><br><span class="line">|- TodoListItemButton.vue</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="在单文件组件、字符模板和JSX中没有内容的组件应该是自闭和的，但是在DOM模板里永远不要这么做"><a href="#在单文件组件、字符模板和JSX中没有内容的组件应该是自闭和的，但是在DOM模板里永远不要这么做" class="headerlink" title="在单文件组件、字符模板和JSX中没有内容的组件应该是自闭和的，但是在DOM模板里永远不要这么做"></a>在单文件组件、字符模板和JSX中没有内容的组件应该是自闭和的，但是在DOM模板里永远不要这么做</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 在单文件组件、字符串模板和 JSX 中 --&gt;</span><br><span class="line">&lt;MyComponent/&gt;</span><br><span class="line">&lt;!-- 在 DOM 模板中 --&gt;</span><br><span class="line">&lt;my-component&gt;&lt;/my-component&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="JS-JSX中的组件名应为大驼峰命名"><a href="#JS-JSX中的组件名应为大驼峰命名" class="headerlink" title="JS/JSX中的组件名应为大驼峰命名"></a>JS/JSX中的组件名应为大驼峰命名</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">Vue.component(<span class="string">&#x27;MyComponent&#x27;</span>, &#123;</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;)</span><br><span class="line">Vue.component(<span class="string">&#x27;my-component&#x27;</span>, &#123;</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">import</span> MyComponent <span class="keyword">from</span> <span class="string">&#x27;./MyComponent.vue&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  name: <span class="string">&#x27;MyComponent&#x27;</span>,</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="多个attribute的元素应该分多行撰写，每个attribute一行"><a href="#多个attribute的元素应该分多行撰写，每个attribute一行" class="headerlink" title="多个attribute的元素应该分多行撰写，每个attribute一行"></a>多个attribute的元素应该分多行撰写，每个attribute一行</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&lt;img</span><br><span class="line">  src=<span class="string">&quot;https://vuejs.org/images/logo.png&quot;</span></span><br><span class="line">  alt=<span class="string">&quot;Vue Logo&quot;</span></span><br><span class="line">&gt;</span><br><span class="line">&lt;MyComponent</span><br><span class="line">  foo=<span class="string">&quot;a&quot;</span></span><br><span class="line">  bar=<span class="string">&quot;b&quot;</span></span><br><span class="line">  baz=<span class="string">&quot;c&quot;</span></span><br><span class="line">/&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="组件模板应该只包含简单的表达式，复杂的表达式应该重构为计算属性或方法"><a href="#组件模板应该只包含简单的表达式，复杂的表达式应该重构为计算属性或方法" class="headerlink" title="组件模板应该只包含简单的表达式，复杂的表达式应该重构为计算属性或方法"></a>组件模板应该只包含简单的表达式，复杂的表达式应该重构为计算属性或方法</h3><p>组件模板应该只包含简单的表达式，复杂的表达式则应该重构为计算属性或方法。复杂表达式会让你的模板变得不那么声明式。我们应该尽量描述应该出现的是什么，而非如何计算那个值。而且计算属性和方法使得代码可以重用</p>
<h3 id="非空的attitude值应该始终带引号"><a href="#非空的attitude值应该始终带引号" class="headerlink" title="非空的attitude值应该始终带引号"></a>非空的attitude值应该始终带引号</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;input type=<span class="string">&quot;text&quot;</span>&gt;</span><br><span class="line">&lt;AppSidebar :style=<span class="string">&quot;&#123; width: sidebarWidth + &#x27;px&#x27; &#125;&quot;</span>&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="指令可以缩写的建议缩写"><a href="#指令可以缩写的建议缩写" class="headerlink" title="指令可以缩写的建议缩写"></a>指令可以缩写的建议缩写</h3><p>指令推荐都使用缩写形式，(用 : 表示 v-bind: 、用 @ 表示 v-on: 和用 # 表示 v-slot:)</p>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity@2.0.4/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.css">
      </div>
      
      
      
    </div>

    
    


</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">郭泽</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js" integrity="sha256-yt2kYMy0w8AbtF89WXb2P1rfjcP/HTHLT7097U8Y5b8=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script>

  
<script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.0/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>

  <script class="next-config" data-name="pdf" type="application/json">{"object_url":{"url":"https://cdnjs.cloudflare.com/ajax/libs/pdfobject/2.2.8/pdfobject.min.js","integrity":"sha256-tu9j5pBilBQrWSDePOOajCUdz6hWsid/lBNzK4KgEPM="},"url":"/lib/pdf/web/viewer.html"}</script>
  <script src="/js/third-party/tags/pdf.js"></script>

  <script class="next-config" data-name="mermaid" type="application/json">{"enable":true,"theme":{"light":"default","dark":"dark"},"js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mermaid/9.1.3/mermaid.min.js","integrity":"sha256-TIYL00Rhw/8WaoUhYTLX9SKIEFdXxg+yMWSLVUbhiLg="}}</script>
  <script src="/js/third-party/tags/mermaid.js"></script>

  <script src="/js/third-party/fancybox.js"></script>


  





</body>
</html>
